package com.hb.hb_advertising;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import com.core.*;
import com.wonders.ips.common.DbConnection;

/**
 * Data access object (DAO) for domain model class HbAdvertising.
 * @see com.hb.hb_advertising.HbAdvertising
 * @author MyEclipse - Hibernate Tools
 */
public class HbAdvertisingDAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(HbAdvertisingDAO.class);

	//property constants
	public static final String BANNERID = "bannerid";
	public static final String USERID = "userid";
	public static final String STATUS = "status";
	public static final String LOCATION = "location";
	
	Transaction tx=null;

    
    public void save(HbAdvertising transientInstance) {
        log.debug("saving HbAdvertising instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().save(transientInstance);
            tx.commit();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(HbAdvertising persistentInstance) {
        log.debug("deleting HbAdvertising instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().delete(persistentInstance);
            tx.commit();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public HbAdvertising findById( java.lang.Integer id) {
        log.debug("getting HbAdvertising instance with id: " + id);
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            HbAdvertising instance = (HbAdvertising) getSession()
                    .get("com.hb.hb_advertising.HbAdvertising", id);
            tx.commit();
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(HbAdvertising instance) {
        log.debug("finding HbAdvertising instance by example");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            List results = getSession()
                    .createCriteria("com.hb.hb_advertising.HbAdvertising")
                    .add(Example.create(instance))
            .list();
            tx.commit();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding HbAdvertising instance with property: " + propertyName
            + ", value: " + value);
      try {
    	  tx=getSession().getTransaction();
      	tx.begin();
         String queryString = "from HbAdvertising as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 tx.commit();
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByBannerid(Object bannerid) {
		return findByProperty(BANNERID, bannerid);
	}
	
	public List findByUserid(Object userid) {
		return findByProperty(USERID, userid);
	}
	
	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}
	
	public List findByLocation(Object location) {
		return findByProperty(LOCATION, location);
	}
	
    public HbAdvertising merge(HbAdvertising detachedInstance) {
        log.debug("merging HbAdvertising instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            HbAdvertising result = (HbAdvertising) getSession()
                    .merge(detachedInstance);
            tx.commit();
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(HbAdvertising instance) {
        log.debug("attaching dirty HbAdvertising instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().saveOrUpdate(instance);
            tx.commit();
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(HbAdvertising instance) {
        log.debug("attaching clean HbAdvertising instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().lock(instance, LockMode.NONE);
            tx.commit();
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    public int checkadving(String bannerid)
    throws SQLException
{
    DbConnection dbcon = null;
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    String sql = "";
    int t=0;
    try
    {
        dbcon = new DbConnection();
        conn = dbcon.getCon();
        stmt = conn.createStatement();
        sql = "select * from hb_advertising where bannerid="+bannerid+" and status=2 and ((select now())>=startdate or startdate is null) and ((select now())<=enddate or enddate is null) order by applydate desc limit 1";
        System.out.println("sql=" + sql);
        rs = stmt.executeQuery(sql);
       

        if(rs.next()){
        	t=1;
        return t;
        }else{
        	t=0;
        return t;
        }
        
    }
    catch(Exception e)
    {
        System.err.println(e.getMessage());
        return 0;
    }
    finally
    {
        rs.close();
        stmt.close();
        conn.close();
    }   
}
}